#ifndef __ALIYUN_CORE_H
#define __ALIYUN_CORE_H

#include <stdbool.h>
#include <stdint.h>

#define TOPIC_PREFIX "/sys"
#define PUB_UP_TOPIC_POSTFIX            "thing/model/up_raw"                    //  "/sys/prokey/devname/thing/model/up_raw"
#define SUB_UP_REPLY_TOPIC_POSTFIX      "thing/model/up_raw_reply"              //  "/sys/prokey/devname/thing/model/up_raw_reply"
#define SUB_DOWN_TOPIC_POSTFIX          "thing/model/down_raw"                  //  "/sys/prokey/devname/thing/model/down_raw"
#define PUB_DOWN_REPLY_TOPIC_POSTFIX    "thing/model/down_raw_reply"            //  "/sys/prokey/devname/thing/model/down_raw_reply"
#define SERVER "a181jbK5wkY.iot-as-mqtt.cn-shanghai.aliyuncs.com"
#define PORT 1883

#define ALI_OOFLINE_RETRY_TIME  (1000*60*10)
#define ALI_REPORT_WAIT_TIMEOUT (1000*15)
#define ALI_PUB_TIMEOUT         (1000*15)



typedef enum
{
    ALI_STATE_OFFLINE,
    ALI_STATE_CONNECTING,
    ALI_STATE_ONLINE,
}ali_state_e;

typedef void (*ali_state_callback)(ali_state_e state);

typedef enum
{

    /* up data,  device->cloud*/
    ALI_MODEL_PROP_REPORT,
    ALI_MODEL_EVENT_REPORT, 
    /* up data , cloud->device*/
    ALI_MODEL_PROP_REPORT_REPLY,
    ALI_MODEL_EVENT_REPORT_REPLY,    
    /* down data, cloud->device*/
    ALI_MODEL_PROP_SET,
    ALI_MODEL_SERVICE_SET, 
    /* down data, device->cloud */
    ALI_MODEL_PROP_SET_REPLY,
    ALI_MODEL_SERVICE_REPLY, 
    /* other*/
    ALI_MODEL_UNKOWN=0XFF,
    
}ali_model_type_e;


typedef struct
{
    ali_model_type_e type;
    bool     is_reply;
    uint32_t id;
    uint8_t *data;
    uint16_t datalen;
}ali_data_t;

bool ali_init(char *prokey,char* devname,char *devsecret);
bool ali_pub_data_queue(ali_model_type_e type,uint32_t id,uint8_t *data,uint16_t len,uint32_t timeout);
bool ali_pub_data_flush(void);
bool ali_state_register(ali_state_callback cb);
ali_state_e ali_state_get(void);

void ali_model_perporty_set_handler(uint8_t *data,uint16_t len);
void ali_model_service_set_handler(uint8_t *data,uint16_t len);


#endif
